//
//  BM_LineView.m
//  BaoYouYou
//
//  Created by MZY on 16/12/4.
//  Copyright © 2016年 caobo56. All rights reserved.
//

#import "BM_LineView.h"

@implementation BM_LineView



-(void)createView{
    _color2 = UIColorFromRGB(0Xe37a7e);
    _color1 = UIColorFromRGB(0X7DE6E2);
    self.frame = CGRectMake(0, 0, WidthOfScreen, 400);
    self.backgroundColor = [UIColor whiteColor];
    int leftW = rateWidth(50);
    int top = 30;
    int height = 40;

    UILabel *s_label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    s_label.text = @"体重(g)";
    s_label.textColor = UIColorFromRGB(0X4c4c4c);
    s_label.font = [UIFont systemFontOfSize:10];
    [s_label sizeToFit];
    s_label.centerX = leftW;
    s_label.centerY = top - 10;
    [self addSubview:s_label];

    UILabel *h_label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    h_label.text = @"未来周数";
    h_label.textColor = UIColorFromRGB(0X4c4c4c);
    h_label.font = [UIFont systemFontOfSize:10];
    [h_label sizeToFit];
    h_label.centerX = WidthOfScreen - leftW -5;
    h_label.top = top+height*5.8+5;
    [self addSubview:h_label];

    if (INCH_4) {
        self.height = 333;
    }else if (INCH_47){
        self.height = 395;
    }else{
        self.height = 421;
    }


    NSArray *colorArray = @[_color2,_color1];
    NSArray *textArray = @[@"参考值",@"预测值"];
    for ( int i = 0; i<2; i++) {
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 1)];
        line.backgroundColor = colorArray[i];
        UIView *yuan = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 5, 5)];
        yuan.backgroundColor = [UIColor whiteColor];
        UIColor *color = colorArray[i];
        yuan.layer.borderColor = color.CGColor;
        yuan.layer.borderWidth = 1;
        yuan.layer.cornerRadius = yuan.width/2;
        yuan.layer.masksToBounds = YES;

        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        label.font = [UIFont systemFontOfSize:11];
        label.text = textArray[i];
        [label sizeToFit];
        label.right = h_label.right;
        label.centerY =s_label.centerY + i*15;
        line.right = label.left - 4;
        line.centerY = label.centerY;
        yuan.centerX = line.centerX;
        yuan.centerY = line.centerY;

        [self addSubview:label];
        [self addSubview:line];
        [self addSubview:yuan];
    }

    float sMax = [_s_array[4] floatValue] - _s_mix;

    for (int i = 1; i<6; i++) {
        UILabel *s_label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        s_label1.text = _s_array[i - 1];
        s_label1.textColor = UIColorFromRGB(0X414141);
        s_label1.font = [UIFont systemFontOfSize:10];
        [s_label1 sizeToFit];
        s_label1.right = leftW - 10;
        s_label1.centerY = top+height*5.8- height*i;
        [self addSubview:s_label1];

        UILabel *h_label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        h_label1.text = [NSString stringWithFormat:@"%d",i - 1];
        h_label1.textColor = UIColorFromRGB(0X4c4c4c);
        h_label1.font = [UIFont systemFontOfSize:10];
        [h_label1 sizeToFit];
        h_label1.top = top+height*5.8+5;
        h_label1.centerX = leftW+leftW*i;
        [self addSubview:h_label1];

        float one = ([_array1[i-1] floatValue] - _s_mix)/sMax;
        float two = ([_array2[i-1] floatValue]-_s_mix)/sMax;

        UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 5, 5)];
        view1.backgroundColor = [UIColor whiteColor];
        view1.layer.borderWidth = 1;
        view1.layer.borderColor = _color1.CGColor;
        view1.layer.cornerRadius = view1.width/2;
        view1.layer.masksToBounds = YES;
        view1.centerX =leftW+leftW*i;
        view1.centerY = top+height*(5.8-one*5);
        [self addSubview:view1];

        UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 10)];
        label1.text = _array1[i-1];
        label1.textColor = _color1;
        label1.font = [UIFont systemFontOfSize:10];
        [label1 sizeToFit];
        label1.centerX = view1.centerX;
        label1.centerY = view1.centerY -10;
        [self addSubview:label1];

        UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 5, 5)];
        view2.backgroundColor = [UIColor whiteColor];
        view2.layer.borderWidth = 1;
        view2.layer.borderColor = _color2.CGColor;
        view2.layer.cornerRadius = view1.width/2;
        view2.layer.masksToBounds = YES;
        view2.centerX =leftW+leftW*i;
        view2.centerY = top+height*(5.8-two*5);
        [self addSubview:view2];

        UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 10)];
        label2.text = _array2[i-1];
        label2.textColor = _color2;
        label2.font = [UIFont systemFontOfSize:10];
        [label2 sizeToFit];
        label2.centerX = view1.centerX;
        label2.centerY = view2.centerY -10;
        [self addSubview:label2];
    }


    UILabel *s_label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    s_label1.text = [NSString stringWithFormat:@"%d",_s_mix];
    s_label1.textColor = UIColorFromRGB(0X414141);
    s_label1.font = [UIFont systemFontOfSize:10];
    [s_label1 sizeToFit];
    s_label1.right = leftW - 10;
    s_label1.centerY = top+height*5.8+5;
    [self addSubview:s_label1];
    [self setNeedsDisplay];
}

-(void)drawRect:(CGRect)rect{

    int leftW = rateWidth(50);
    int top = 30;
    int height = 40;

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL,leftW,top);
    CGPathAddLineToPoint(path, NULL,leftW, top+height*5.8);
    CGPathAddLineToPoint(path, NULL,WidthOfScreen - leftW, top+height*5.8);

    for (int i = 1; i<6; i++) {
        CGPathMoveToPoint(path, NULL,leftW+leftW*i,top+height*5.8);
        CGPathAddLineToPoint(path, NULL,leftW+leftW*i,top+height*5.8 - 5);

        CGPathMoveToPoint(path, NULL,leftW,top+height*5.8- height*i);
        CGPathAddLineToPoint(path, NULL,leftW+5,top+height*5.8- height*i);
    }

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, UIColorFromRGB(0Xe37a7e).CGColor);
    CGContextAddPath(context, path);
    CGContextStrokePath(context);

    CGMutablePathRef path_31 = CGPathCreateMutable();
    CGPathMoveToPoint(path_31, NULL,leftW,top);
    CGPathAddLineToPoint(path_31, NULL,leftW-5,top+8);
    CGPathAddLineToPoint(path_31, NULL,leftW+5,top+8);
    CGPathAddLineToPoint(path_31, NULL,leftW,top);
    CGPathCloseSubpath(path_31);
    CGContextAddPath(context, path_31);
    CGContextSetFillColorWithColor(context, UIColorFromRGB(0Xe37a7e).CGColor);
    CGContextFillPath(context);

    CGMutablePathRef path_32 = CGPathCreateMutable();
    CGPathMoveToPoint(path_32, NULL,WidthOfScreen - leftW, top+height*5.8);
    CGPathAddLineToPoint(path_32, NULL,WidthOfScreen - leftW-8, top+height*5.8+5);
    CGPathAddLineToPoint(path_32, NULL,WidthOfScreen - leftW-8, top+height*5.8-5);
    CGPathAddLineToPoint(path_32, NULL,WidthOfScreen - leftW, top+height*5.8);
    CGPathCloseSubpath(path_32);
    CGContextAddPath(context, path_32);
    CGContextSetFillColorWithColor(context, UIColorFromRGB(0Xe37a7e).CGColor);
    CGContextFillPath(context);


    CGMutablePathRef path_array1 = CGPathCreateMutable();
    CGMutablePathRef path_array2 = CGPathCreateMutable();

    float sMax = [_s_array[4] floatValue] - _s_mix;
    float one = ([_array1[0] floatValue] - _s_mix)/sMax;
    float two = ([_array2[0] floatValue]-_s_mix)/sMax;

    CGPathMoveToPoint(path_array1, NULL,leftW+leftW,top+height*(5.8-one*5));
    CGPathMoveToPoint(path_array2, NULL,leftW+leftW,top+height*(5.8-two*5));

    for (int i = 1; i<5; i++) {
        float one = ([_array1[i] floatValue] - _s_mix)/sMax;
        float two = ([_array2[i] floatValue]-_s_mix)/sMax;
        CGPathAddLineToPoint(path_array1, NULL,leftW+leftW*(i+1),top+height*(5.8-one*5));
        CGPathAddLineToPoint(path_array2, NULL,leftW+leftW*(i+1),top+height*(5.8-two*5));
    }
    CGContextRef context2 = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context2, _color1.CGColor);
    CGContextAddPath(context2, path_array1);
    CGContextStrokePath(context2);

    CGContextRef context3 = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context3, _color2.CGColor);

    CGContextAddPath(context3, path_array2);
    CGContextStrokePath(context3);


}@end
